﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.Dataprogram;
using System.Windows;
using BLL.TcpUdp;
using System.Threading;
using Framework.Kit;

namespace Client
{
    public delegate void GreetingDelegate(string id);
    /// <summary>
    /// 系统命令处理
    /// </summary>
    public class commandCtr
    {
        commandType cmdType;
        string data;

        DataproAnaly dataAny=new DataproAnaly();
        /// <summary>
        /// 出价成功事件后要修改页面
        /// </summary>
        public GreetingDelegate add;
              
        /// <summary>
        /// 出现新的最高价
        /// </summary>
        public GreetingDelegate newmoney;

        /// <summary>
        /// 初始化函数，要手动加载（加载前可以各种邦定事件）
        /// </summary>
        public void commandCtra(NetEventArgs e)
        {
            try
            {
                dataAny.uncodeData(e.Client.Datagram);
                this.cmdType = dataAny.CmdType;
                this.data = dataAny.DataBody;
                analyCmd(e);
            }
            catch (Exception ex)
            {
                other.WriteErrLog("初始化commandCtra函数",ex);
            }
        }


        public void analyCmd(NetEventArgs e)
        {
            /*******是否需要返回当前的请求信息*******/
            bool isCondition=true;
            commandType cmdType=commandType.c000;
            string databBody="";

            switch (this.cmdType)
            {
                case commandType.c000:
                    isCondition = false;
                    break;
                case commandType.c004:
                    isCondition = false;
                    App.islogin = true;
                    break;
                case commandType.c005:
                    isCondition = false;
                    MessageBox.Show("登录失败:"+data);
                    break;
                case commandType.c002:
                    App.seatNum = data;
                    isCondition = false;
                    break;
                case commandType.c007:
                    this.add(data);
                    isCondition = false;
                    break;
                case commandType.c008:
                    string[] arr1 = data.Split('|');  // 字符串变成数组
                    App.maxmoney =int.Parse(arr1[0]);
                    this.newmoney(data);
                    isCondition = false;
                    break;
                default:
                    isCondition = false;
                    cmdType = commandType.c000;
                    databBody = "无效命令";
                    break;
            }

            //回复客户端信息
            if(isCondition){
                try
                {
                    App.sendData(cmdType, databBody);
                }
                catch (Exception ex)
                {
                    other.WriteErrLog("客户端回返信息", ex);
                }
            }
        }
    }
}
